<template>
  <a-modal
    title="开通ERP账号"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
    style="top: 20px"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" >
        <a-form-item label="用户账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入用户账号" v-decorator="['username', {}]" :readOnly="true" />
        </a-form-item>

        <a-form-item label="开通类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['thirdType', validatorRules.thirdType]">
            <a-radio value="create">新建</a-radio>
            <a-radio value="bind">绑定已有账号</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item label="ERP用户账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            placeholder="请填写11位手机号"
            v-decorator="['thirdId', validatorRules.thirdId]"
          />说明：开通成功后，将用这个账号登录ERP
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { changePassword1,openErpUser } from '@/api/api'

export default {
  name: 'ErpModal',
  data() {
    return {
      visible: false,
      confirmLoading: false,
      confirmDirty: false,
      validatorRules: {
        thirdId: {
          rules: [
            {
              required: true,
              message: '必填项!',
            }
          ],
        },
        thirdType: {
          rules: [
            {
              required: true,
              message: '必填项!',
            }
          ],
        },
      },
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      form: this.$form.createForm(this),
    }
  },
  created() {
    console.log('created')
  },

  methods: {
    show(record) {
      this.form.resetFields()
      this.visible = true
      this.model.username = record.username
      this.model.id = record.id
      this.model.thirdId = record.thirdId
      this.model.thirdType = record.thirdType
      this.model.nickName = record.realname
      this.$nextTick(() => {
        this.form.setFieldsValue({ username: record.username, thirdId: record.thirdId, thirdType: record.thirdType })
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disableSubmit = false
      this.selectedRole = []
    },
    handleSubmit() {
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          let formData = Object.assign(this.model, values)
          openErpUser(formData)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.error(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>